package com.krtech.wecard.module.ramble.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.ramble.entity.RambleAccept;
import com.krtech.wecard.module.ramble.entity.RambleLoginLog;
import com.krtech.wecard.module.ramble.enums.RambleEnums;
import com.krtech.wecard.module.ramble.service.RambleAcceptService;
import com.krtech.wecard.module.ramble.service.RambleLoginLogService;
import com.krtech.wecard.module.sys.entity.SysKeyValue;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.service.SysKeyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.krtech.wecard.module.ramble.entity.RambleDrawLog;
import com.krtech.wecard.module.ramble.mapper.RambleDrawLogMapper;
import com.krtech.wecard.module.ramble.service.RambleDrawLogService;

@Service
public class RambleDrawLogServiceImpl extends ServiceImpl<RambleDrawLogMapper, RambleDrawLog> implements RambleDrawLogService {

    @Autowired
    private SysKeyValueService keyValueService;

    @Autowired
    private RambleLoginLogService loginLogService;

    @Autowired
    private RambleAcceptService acceptService;

    @Override
    public int getCanAcceptCount() {
        SysUser userInfo = LoginUtil.getUserInfo();

        String today = DateUtil.today();
        //查询环境变量
        SysKeyValue stepKeyValue = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, RambleEnums.STEP));

        if (stepKeyValue == null){
            throw new BusinessException("环境变量step未配置，请联系管理员");
        }

        int step = Integer.parseInt(stepKeyValue.getSysValue());

        List<RambleAccept> totalAcceptList = acceptService.list(new QueryWrapper<RambleAccept>()
                .eq(RambleAccept.COL_ACCEPT_DATE,today)
                .eq(RambleAccept.COL_PHONE, userInfo.getPhone()));

        List<RambleLoginLog> loginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>()
                .eq(RambleLoginLog.COL_RIGISTER_DATE,today)
                .eq(RambleLoginLog.COL_PHONE, userInfo.getPhone()));
        //剩余步数
        int remainStep = 0;
        //领了几张
        int totalStep = loginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();

        remainStep = totalStep - (totalAcceptList.size() * step);

        //计算可领次数
        int prizeCount = remainStep / step;

        if (prizeCount > 1){
            return 1;
        }

        return prizeCount;
    }

    @Override
    public int getCanAcceptCount(String phone) {
        //查询环境变量
        SysKeyValue stepKeyValue = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, RambleEnums.STEP));

        if (stepKeyValue == null){
            throw new BusinessException("环境变量step未配置，请联系管理员");
        }

        int step = Integer.parseInt(stepKeyValue.getSysValue());

        List<RambleAccept> totalAcceptList = acceptService.list(new QueryWrapper<RambleAccept>()
                .eq(RambleAccept.COL_PHONE, phone));

        List<RambleLoginLog> loginLogList = loginLogService.list(new QueryWrapper<RambleLoginLog>().eq(RambleLoginLog.COL_PHONE, phone));
        //剩余步数
        int remainStep = 0;
        //领了几张
        int totalStep = loginLogList.stream().mapToInt(RambleLoginLog::getStepNumber).sum();

        remainStep = totalStep - (totalAcceptList.size() * step);

        //计算可领次数
        int prizeCount = remainStep / step;
        return prizeCount;
    }
}

